package com.wss.module.market.ui.main;

import com.felipecsl.asymmetricgridview.AsymmetricRecyclerView;
import com.felipecsl.asymmetricgridview.AsymmetricRecyclerViewAdapter;
import com.orhanobut.logger.Logger;
import com.wss.common.base.BaseActionBarAbility;
import com.wss.common.base.adapter.listener.OnListItemClickListener;
import com.wss.common.bean.Event;
import com.wss.common.constants.Dic;
import com.wss.common.constants.EventAction;
import com.wss.common.manage.AbilityToAbility;
import com.wss.common.utils.PxUtils;
import com.wss.common.utils.TextUtils;
import com.wss.module.market.ResourceTable;
import com.wss.module.market.bean.GoodsInfo;
import com.wss.module.market.ui.goods.cart.ShoppingCartAbility;
import com.wss.module.market.ui.main.adapter.MarketMainProvider;
import com.wss.module.market.ui.main.mvp.MarketMainPresenter;
import com.wss.module.market.ui.main.mvp.contract.MarketMainContract;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;

import java.util.*;

public class MarkerMainAbility extends BaseActionBarAbility<MarketMainPresenter>
        implements MarketMainContract.View {

    private AsymmetricRecyclerView recyclerView;

    private Text tvCount;

    private MarketMainProvider adapter;
    private List<GoodsInfo> marketInfoList = new ArrayList<>();

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        initView();
    }

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_ability_marker_main;
    }

    @Override
    protected MarketMainPresenter createPresenter() {
        return new MarketMainPresenter();
    }

    @Override
    public void refreshGoodsList(List<GoodsInfo> dataList) {
        adapter.appendItems(dataList);
        Logger.e("==== marker main ability refresh goods list ..... data list : " + adapter.getCount());
    }

    @Override
    public void refreshCartCount(long count) {

        System.out.println("==== refresh cart count : " + count);

        if (count > 0) {
            tvCount.setVisibility(Component.VISIBLE);
            tvCount.setText(String.valueOf(count));
        } else {
            tvCount.setVisibility(Component.HIDE);
        }
    }

    @Override
    public void onEventBus(Event event) {
        super.onEventBus(event);
        if (TextUtils.equals(EventAction.EVENT_SHOPPING_CART_CHANGED, event.getAction()) ||
                TextUtils.equals(EventAction.EVENT_SHOPPING_CART_REFRESH, event.getAction())) {
            //刷新购物车
            getPresenter().getCartCount();
        }
    }

    @Override
    protected boolean registerEventBus() {
        return true;
    }

    private void initView() {
        setCenterText("商城模板");
        recyclerView = (AsymmetricRecyclerView) findComponentById(ResourceTable.Id_rv_list);
        recyclerView.setRequestedColumnCount(2);

        adapter = new MarketMainProvider(context, marketInfoList, new OnListItemClickListener<GoodsInfo>() {
            @Override
            public void onItemClick(GoodsInfo data, int position) {

                Map<String, Object> param = new HashMap<>();
                param.put(Dic.GOODS_INFO, marketInfoList.get(position));
//                AbilityToAbility.toAbility(context, GoodsDetailAbility.class, param);
            }
        });

        recyclerView.setItemProvider(new AsymmetricRecyclerViewAdapter<>(this, recyclerView, adapter));

        Component cartView = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_market_layout_market_main_shopping_cart, null, false);
        tvCount = (Text) cartView.findComponentById(ResourceTable.Id_tv_count);
        cartView.findComponentById(ResourceTable.Id_iv_cart).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                AbilityToAbility.toAbility(context, ShoppingCartAbility.class);
            }
        });

        getTitleBar().setRightView(cartView);
        getPresenter().start();
    }
}
